package main

import (
	"github.com/xuri/excelize"
	"fmt"
	"match-spider/util/boltdb"
	"github.com/boltdb/bolt"
	"strings"
	"strconv"
	"encoding/json"
	"sync"
)

/**
导出比赛数据到excel

 */

func all( fn func(map[string][]string,int))  {
	num:=0
	company:=[]string{
		"澳门","竞彩官方","金宝博(马恩岛)","bet 365(英国)","Oddset(德国)","威廉希尔(英国)","Interwetten(塞浦路斯)","香港马会(中国香港)",
		"立博(英国)","SNAI(意大利)",
	}
	var wg sync.WaitGroup
	boltdb.ScanTx("matchs-2016", func(tx *bolt.Tx) {

		tx.ForEach(func(name []byte, b *bolt.Bucket) error {
			b.ForEach(func(k, v []byte) error {
				content:=string(v)
				s:=strings.Split(content,"^")
				league:=s[1]
				if league=="英甲"{

					m:=map[string][]string{
						"match":s,
					}
					wg.Add(3)
					boltdb.Get("asia-2016",league,string(k), func(data string) {
						if data!=""{

							odds:=strings.Split(data,"!")
							for _,v:=range odds {
								d := strings.Split(v, "^")
								name := d[0]
								slice:=[]string{d[2],d[3],d[4],d[5],d[6],d[7],}
								if strings.Compare(name,"澳门")==0{
									m["澳门(亚)"]=slice
								}
								if strings.Compare(name,"易胜博(安提瓜和巴布达)")==0{
									m["易胜博(安提瓜和巴布达)(亚)"]=slice
								}
							}


						}

						company1:=[]string{"澳门(亚)","易胜博(安提瓜和巴布达)(亚)"}
						for _,v:=range company1{
							if m[v]==nil{
								m[v]=[]string{
									"","","","","","",
								}
							}
						}
						wg.Add(-1)
					})
					boltdb.Get("europe-2016",league,string(k), func(data string) {
						if data!=""{
							num++
							odds:=make([]string,0)
							json.Unmarshal([]byte(data),&odds)
							for _,v:=range odds {

								d := strings.Split(v, "|")
								name := d[21]
								slice:=[]string{d[3],d[4],d[5],d[10],d[11],d[12],}
								if strings.Compare(name,"澳门")==0{
									m["澳门"]=slice
								}
								if strings.Compare(name,"竞彩官方")==0{
									m["竞彩官方"]=slice
								}
								if strings.Compare(name,"金宝博(马恩岛)")==0{
									m["金宝博(马恩岛)"]=slice
								}
								if strings.Compare(name,"bet 365(英国)")==0{
									m["bet 365(英国)"]=slice
								}
								if strings.Compare(name,"Oddset(德国)")==0{
									m["Oddset(德国)"]=slice
								}
								if strings.Compare(name,"威廉希尔(英国)")==0{
									m["威廉希尔(英国)"]=slice
								}
								if strings.Compare(name,"Interwetten(塞浦路斯)")==0{
									m["Interwetten(塞浦路斯)"]=slice
								}
								if strings.Compare(name,"香港马会(中国香港)")==0{
									m["香港马会(中国香港)"]=slice
								}
								if strings.Compare(name,"立博(英国)")==0{
									m["立博(英国)"]=slice
								}
								if strings.Compare(name,"SNAI(意大利)")==0{
									m["SNAI(意大利)"]=slice
								}

							}
							for _,v:=range company{
								if m[v]==nil{
									m[v]=[]string{
										"","","","","","",
									}
								}
							}

						}
						wg.Add(-1)
					})
					boltdb.Get("overunder-2016",league,string(k), func(data string) {
						if data!=""{

							odds:=strings.Split(data,"!")
							for _,v:=range odds {
								d := strings.Split(v, "^")
								name := d[0]
								slice:=[]string{d[2],d[3],d[4],d[5],d[6],d[7],}
								if strings.Compare(name,"澳门")==0{
									m["澳门(大小)"]=slice
								}

								if strings.Compare(name,"Bet365")==0{
									m["Bet365(大小)"]=slice
								}

								if strings.Compare(name,"韦德")==0{
									m["韦德(大小)"]=slice
								}

							}

						}
						company1:=[]string{"澳门(大小)","Bet365(大小)","韦德(大小)"}
						for _,v:=range company1{
							if m[v]==nil{
								m[v]=[]string{
									"","","","","","",
								}
							}
						}
						wg.Add(-1)

					})

					wg.Wait()

					fn(m,num)
				}

				return nil
			})
			return nil
		})

	})
	fmt.Println(num)
}
func main()  {

	e:=[]string{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
	xlsx := excelize.NewFile()

	categories := []string{
		"编号",
		"联赛",
		"时间",
		"主队",
		"进球",
		"客队",
		"进球",
		"亚盘",
		"大小",
		"(欧)澳门",
		"",
		"",
		"",
		"",
		"",
		"(欧)竞彩官方",
		"",
		"",
		"",
		"",
		"",
		"(欧)金宝博(马恩岛)",//
		"",
		"",
		"",
		"",
		"",
		"(欧)bet 365(英国)",//
		"",
		"",
		"",
		"",
		"",
		"(欧)Oddset(德国)",//
		"",
		"",
		"",
		"",
		"",
		"(欧)威廉希尔(英国)",//
		"",
		"",
		"",
		"",
		"",
		"(欧)Interwetten(塞浦路斯)",//
		"",
		"",
		"",
		"",
		"",
		"(欧)香港马会(中国香港)",//
		"",
		"",
		"",
		"",
		"",
		"(欧)立博(英国)",
		"",
		"",
		"",
		"",
		"",
		"(欧)SNAI(意大利)",
		"",
		"",
		"",
		"",
		"",
		"(亚)澳门",
		"",
		"",
		"",
		"",
		"",
		"(亚)易胜博(安提瓜和巴布达)",
		"",
		"",
		"",
		"",
		"",
		"(大小)澳门",
		"",
		"",
		"",
		"",
		"",
		"(大小)Bet365",
		"",
		"",
		"",
		"",
		"",
		"(大小)韦德",
		"",
		"",
		"",
		"",
		"",

	}

	for k,v:=range categories{
		len:=int(k/26)
		s:=""
		if len>0{
			s+=e[len-1]+e[k%26]
		}else{
			s+=e[k%26]
		}

		xlsx.SetCellValue("Sheet1",s+"1", v)
	}
	all(func(data map[string][]string,i int) {
		match:=data["match"]
		odds1:=data["澳门"]
		odds2:=data["竞彩官方"]
		odds3:=data["金宝博(马恩岛)"]
		odds4:=data["bet 365(英国)"]
		odds5:=data["Oddset(德国)"]
		odds6:=data["威廉希尔(英国)"]
		odds7:=data["Interwetten(塞浦路斯)"]
		odds8:=data["香港马会(中国香港)"]
		odds9:=data["立博(英国)"]
		odds10:=data["SNAI(意大利)"]

		odds11:=data["澳门(亚)"]
		odds12:=data["易胜博(安提瓜和巴布达)(亚)"]
		odds13:=data["澳门(大小)"]
		odds14:=data["Bet365(大小)"]
		odds15:=data["韦德(大小)"]
		slice:=[][]string{match,odds1,odds2,odds3,odds4,odds5,odds6,odds7,odds8,odds9,odds10,odds11,odds12,odds13,odds14,odds15}
		s:=[]string{}
		for _,v:=range slice{
			for _,v1:=range v{
				s=append(s,v1)
			}


		}
		for k,v:=range s{
			len:=int(k/26)
			s:=""
			if len>0{
				s+=e[len-1]+e[k%26]
			}else{
				s+=e[k%26]
			}
			xlsx.SetCellValue("Sheet1",s+strconv.Itoa(i+1), v)
		}

	})
	err := xlsx.SaveAs("./Workbook.xlsx")
	if err != nil {
		fmt.Println(err)
	}
}